var Banners = (function () {
	function handleElements() {}

	function handleGrid() {
		var boolRender = function (v, r, i) {
			var color = v == '1' ? 'text-success' : 'text-danger';
			return '<i class="icon-circle ' + color + '"></i>';
		},
			priceRender = function (v) {
				return $.number(v);
			};
		$('#grid').datagrid({
			url: siteURL('banners/load_data'),
			title: ' Banners List',
			height: 400,
			rownumbers: true,
			pagination: true,
			pagePosition: 'top',
			autoRowHeight: false,
			singleSelect: true,
			columns: [[{
				field: 'b_name',
				title: 'Name',
				width: 150,
				sortable: true
			}, {
				field: 'b_content',
				title: 'Content',
				width: 300,
				sortable: true
			}, {
				field: 'b_order',
				title: 'Order',
				width: 80,
				align: 'center'
			}, {
				field: 'b_url',
				title: 'Target',
				width: 200,
				align: 'center'
			}]]
		});
		var pager = $('#grid').treegrid('getPager');
		pager.pagination({
			buttons: [
				{
					text: '<i class="icon-plus"></i> Add',
					handler: addRecord
				},
				{
					text: '<i class="icon-pencil"></i> Edit',
					handler: editRecord
				},
				{
					text: '<i class="icon-trash"></i> Delete',
					handler: deleteRecord
				},
				'-',
				{
					text: '<i class="icon-picture"></i> View Image',
					handler: viewGalery
				}
			]
		});
	}

	function handleForm() {}

	function handleWindow() {
		$('#win').show();
		$('#win').dialog({
			modal: true,
			width: 420,
			title: ' Banner Form',
			buttons: [{
				text: 'Submit',
				handler: function () {
					$('#form').form('submit', {
						url: siteURL('banners/save_data'),
						success: function () {
							$('#win').dialog('close');
							$('#grid').datagrid('reload');
						}
					});
				}
			},{
				text: 'Close',
				handler: function () {
					$('#win').dialog('close');
				}
			}]
		});
		$('#win').dialog('close');
		$('#gal').show();
		$('#gal').dialog({
			modal: true,
			width: $('.grid-container .panel.datagrid').width() / 1.3,
			height: 420,
			title: 'Banner Galery',
			onOpen: function () {
				$(this).find('.dialog-button a.l-btn').first().show();
			},
			onLoad: function () {
				var uploader = $(this).find('#uploader');
				if (uploader.length) {
					handleUpload();
					$(this).find('.dialog-button a.l-btn').first().hide();
				} else {
					$(this).find('.btn-delete-image').on('click', deleteImage);
				}
			},
			buttons: [
				{
					text: '<i class="icon-picture"></i> Upload Image',
					handler: uploadImage
				},
				{
					text: 'Close',
					handler: function () {
						$('#gal').dialog('close');
						$('#grid').datagrid('reload');
					}
				}
			]
		});
		$('#gal').dialog('close');
	}

	function addRecord() {
		$('#win').dialog('open');
		$('#form').form('clear').form('reset');
		$('#longdesc').code('');
	}

	function editRecord() {
		var record = $('#grid').datagrid('getSelected');
		if (!record) {
			$.messager.alert('Warning', 'No record selected', 'warning');
			return;
		}
		$('#win').dialog('open');
		$('#form').form('load', record);
	}

	function deleteRecord() {
		var record = $('#grid').datagrid('getSelected');
		if (!record) {
			$.messager.alert('Warning', 'No record selected', 'warning');
			return;
		}
		$.messager.confirm('Confirm', 'Delete selected record?', function (r) {
			if (r) {
				$.ajax({
					url: siteURL('banners/delete_data'),
					type: 'post',
					dataType: 'json',
					data: record
				}).done(function () {
					$('#grid').datagrid('reload');
				});
			}
		});
	}

	function viewGalery() {
		var record = $('#grid').datagrid('getSelected');
		if (!record) {
			$.messager.alert('Warning', 'No record selected', 'warning');
			return;
		}
		$('#gal').dialog('open');
		$('#gal').dialog('refresh', siteURL('banners/load_images/' + record.b_id));
	}

	function uploadImage() {
		$('#gal').dialog('refresh', siteURL('banners/banners_upload'));
	}

	function handleUpload() {
		var record = $('#grid').datagrid('getSelected');
		$('#fileupload').fileupload({
			url: siteURL('banners/banners_upload/upload_img/' + record.b_id)
		});
	}

	function deleteImage(e) {
		e.preventDefault();
		var t = $(e.target),
			id = t.data('id'),
			file = t.data('file');
		$.messager.confirm('Confirm', 'Delete image?', function (r) {
			if (r) {
				$.ajax({
					url: siteURL('banners/delete_image'),
					type: 'post',
					dataType: 'json',
					data: {
						pi_id: id,
						pi_filename: file
					}
				}).done(function () {
					$('#gal').dialog('refresh');
				});
			}
		});
	}

	function handleSearch() {
		$('#omnisearch').on('keypress', function (e) {
			if (e.keyCode === 13) {
				var query = $(this).val();
				$('#grid').datagrid('load', {
					query: query
				});
			}
		});
	}
	
	return {
		init: function () {
			handleElements();
			handleGrid();
			handleForm();
			handleWindow();
			handleSearch();
		}
	};
}(jQuery));
$(document).ready(Banners.init);